package servlets;

import beans.User_data;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

/**
 *
 * @author Aspire
 */
@WebServlet(name = "FBAction", urlPatterns = {"/FBAction.do"})
public class FBAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            String user_id = request.getParameter("user_id");
            String name = request.getParameter("name");
            String gender = request.getParameter("gender");
            String x1 = request.getParameter("x1");
            String x2 = request.getParameter("x2");
            String email = request.getParameter("email");
            String bday = request.getParameter("birthday");
            //String bday=request.getParameter("user_likes_ids");
            String response_list = request.getParameter("user_likes_ids");
            JSONArray arr = null;
            try {
                arr = (JSONArray) new JSONParser().parse(response_list);
                System.out.println("name : " + name);
                System.out.println("gender : " + gender);
                System.out.println("array size : " + arr.size());
                System.out.println("x1= " + x1 + " x2= " + x2);

                String test = arr.toString();
                String category_likes_string = test.substring(test.indexOf('[') + 1, test.indexOf(']'));
                System.out.println("String : " + category_likes_string);
                User_data u = new User_data();
                HttpSession session = request.getSession(true);
                u.setId(user_id);
                u.setName(name);
                u.setEmail(email);
                u.setGender(gender);
                u.setBirthday(bday);
                u.setUser_likes_ids(category_likes_string);
                session.setAttribute("user", u);
//            if(u.register_user()==false){
//                u.matchAction();
//            }
//            response.sendRedirect("filter.jsp");
                System.out.println("success : " + u.registerUser());
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
